package cn.icare.cms.task;

import cn.icare.cms.OrderService;
import cn.icare.cms.controller.BaseController;
import cn.icare.cms.controller.ItemOrderController;
import cn.icare.cms.dao.po.Order;
import cn.icare.cms.model.enums.OrderStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Classname OrderTask
 * @Description TODO 订单定时任务类
 * @Date 2020/4/1 17:33
 * @Created by Administrator
 */
@Component
public class OrderTask extends BaseController {


    @Autowired
    private OrderService orderService;


    private final Logger logger = LoggerFactory.getLogger(OrderTask.class);

    /**
     * 定时取消，未付款及下单时间超过30分钟的订单
     * 定时策略每分钟执行
     */
    @Scheduled(cron = "0 0/1 * * * ? ")   //每分钟执行一次
    @PostConstruct //系统启动后，自动开始执行此方法
    public void auto_cancle() {
        this.logger.info("订单自动取消任务开始执行");
        List<Order> orderList = this.orderService.getNeedCancelList();
        if (orderList != null && orderList.size() > 0) {
            orderList.parallelStream().forEach(item -> {
                Long id = item.getId();
                this.orderService.updateStatus(id, OrderStatusEnum.CANCEL.getCode());
                this.resumeOrderItemStore(id);
            });
            this.logger.info(String.format("订单自动取消任务数：%s", orderList.size()));
        }
        this.logger.info("订单自动取消任务执行完成");
    }
}
